home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Tools / Freeware / ttengine-5.0 / Examples / TextLength / txlen.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-05  |  5.7 KB  |  167 lines

  1. /* test ttrender */
  2.  
  3. #define __NOLIBBASE__
  4.  
  5. #include <proto/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/ttengine.h>
  10. #include <proto/asl.h>
  11.  
  12. #include <libraries/ttengine.h>
  13.  
  14. extern struct Library *SysBase, *DOSBase;
  15.  
  16. struct Library *TTEngineBase, *IntuitionBase, *GfxBase, *AslBase;
  17.  
  18. /*----------------------------------------------------------------------------------------------------*/
  19.  
  20. static STRPTR get_font_name(struct Library *AslBase)
  21.   {
  22.     struct FileRequester *freq;
  23.     STRPTR name = NULL;
  24.  
  25.     if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
  26.       {
  27.         if (AslRequestTags(freq,
  28.           ASLFR_TitleText, (ULONG)"Select TrueType font",
  29.           ASLFR_InitialDrawer, (ULONG)"FONTS:",
  30.           ASLFR_DoPatterns, TRUE,
  31.           ASLFR_InitialPattern, (ULONG)"#?.ttf",
  32.           ASLFR_RejectIcons, TRUE,
  33.           TAG_END))
  34.           {
  35.             ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;
  36.  
  37.             if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
  38.               {
  39.                 strncpy(name, freq->fr_Drawer, namelen);
  40.                 AddPart(name, freq->fr_File, namelen);
  41.               }
  42.           }
  43.         FreeAslRequest(freq);
  44.       }
  45.     return name;
  46.   }
  47.  
  48. /*----------------------------------------------------------------------------------------------------*/
  49.  
  50. static VOID free_font_name(STRPTR name)
  51.   {
  52.     if (name) FreeVec(name);
  53.   }
  54.  
  55. /*----------------------------------------------------------------------------------------------------*/
  56.  
  57. void measured_text(STRPTR text, UWORD y, struct RastPort *rp)
  58.   {
  59.     struct TextExtent te;
  60.     UWORD len;
  61.     ULONG pixlen;
  62.  
  63.     len = strlen(text);
  64.     pixlen = TT_TextLength(rp, text, len);
  65.  
  66.     SetAPen(rp, 2);
  67.     Move(rp, 10, y);
  68.     Draw(rp, 10, y + 7);
  69.     Move(rp, 10, y + 5);
  70.     Draw(rp, 10 + pixlen, y + 5);
  71.     Move(rp, 10 + pixlen, y);
  72.     Draw(rp, 10 + pixlen, y + 7);
  73.  
  74.     SetAPen(rp, 1);
  75.     Move(rp, 10, y);
  76.     TT_Text(rp, text, len);
  77.   }
  78.  
  79.  
  80. int Main (void)
  81.   {
  82.     struct Window *win;
  83.     STRPTR fontname;
  84.  
  85.     if (GfxBase = OpenLibrary("graphics.library", 39))
  86.       {
  87.         if (IntuitionBase = OpenLibrary("intuition.library", 39))
  88.           {
  89.             if (AslBase = OpenLibrary("asl.library", 38))
  90.               {
  91.                 if (fontname = get_font_name(AslBase))
  92.                   {
  93.                     if (TTEngineBase = OpenLibrary("ttengine.library", 0))
  94.                       {
  95.                         if (win = OpenWindowTags(NULL,
  96.                           WA_Top, 25,
  97.                           WA_Left, 0,
  98.                           WA_Width, 640,
  99.                           WA_Height, 210,
  100.                           WA_CloseGadget, TRUE,
  101.                           WA_DragBar, TRUE,
  102.                           WA_DepthGadget, TRUE,
  103.                           WA_IDCMP, IDCMP_CLOSEWINDOW,
  104.                           WA_Title, (ULONG)"TT_TextLength() test",
  105.                           TAG_END))
  106.                           {
  107.                             ULONG sigmask, signals;
  108.                             BOOL running = TRUE;
  109.                             struct RastPort *rp = win->RPort;
  110.                             APTR font;
  111.  
  112.                             if (font = TT_OpenFont(
  113.                               TT_FontFile, (ULONG)fontname,
  114.                               TT_FontSize, 18,
  115.                               TT_ScaleX, 0x3FA28F5C,
  116.                             TAG_END))
  117.                               {
  118.                                 if (TT_SetFont(rp, font))
  119.                                   {
  120.                                     TT_SetAttrs(rp,
  121.                                       TT_Window, (ULONG)win,
  122.                                     TAG_END);
  123.  
  124.                                     SetDrMd(rp, JAM1);
  125.  
  126.                                     measured_text("TT_TextLength() example", 45, rp);
  127.                                     measured_text("White lines shows text length in pixels as computed by the function.", 70, rp);
  128.                                     measured_text("Note that TT_TextLength() returns only \"pen advance\".", 95, rp);
  129.                                     measured_text("Use TT_TextExtent() if you want to get precise bounding box.", 120, rp);
  130.                                   }
  131.                                 else PutStr("TT_SetFont() failed.\n");
  132.                                 TT_CloseFont(font);
  133.                               }
  134.                             else PutStr("Font open failed.\n");
  135.  
  136.                             sigmask = SIGBREAKF_CTRL_C | (1 << win->UserPort->mp_SigBit);
  137.                             while (running)
  138.                               {
  139.                                 signals = Wait(sigmask);
  140.                                 if (signals & SIGBREAKF_CTRL_C) running = FALSE;
  141.                                 if (signals & (1 << win->UserPort->mp_SigBit))
  142.                                   {
  143.                                     struct IntuiMessage *imsg;
  144.  
  145.                                     while (imsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  146.                                       {
  147.                                         if (imsg->Class == IDCMP_CLOSEWINDOW) running = FALSE;
  148.                                         ReplyMsg((struct Message*)imsg);
  149.                                       }
  150.                                   }
  151.                               }
  152.                             TT_DoneRastPort(win->RPort);
  153.                             CloseWindow(win);
  154.                           }
  155.                         CloseLibrary(TTEngineBase);
  156.                       }
  157.                     free_font_name(fontname);
  158.                   }
  159.                 CloseLibrary(AslBase);
  160.               }
  161.             CloseLibrary(IntuitionBase);
  162.           }
  163.         CloseLibrary(GfxBase);
  164.       }
  165.     return 0;
  166.   }
  167.